﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.HVAC.Properties;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public partial class LayAreaFormNew : System.Windows.Forms.Form
	{
		public LayAreaFormNew(HVACUserSetting uSetting)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.userSetting = uSetting;
		}

		private void rdoLayLine_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoLayLine.Checked)
			{
				this.SetControlDisplay("LayLine");
			}
		}

		private void SetControlDisplay(string type)
		{
			if (type == "LayLine")
			{
				this.gbLayLine.Visible = true;
				this.gbLayRect.Visible = false;
				this.gbLayCross.Visible = false;
				this.tabcLayMid.Visible = false;
				this.label25.Visible = false;
				this.hytbRowAngle.Visible = false;
				return;
			}
			if (type == "LayCross")
			{
				this.gbLayLine.Visible = false;
				this.gbLayRect.Visible = false;
				this.gbLayCross.Visible = true;
				this.tabcLayMid.Visible = false;
				this.label25.Visible = false;
				this.hytbRowAngle.Visible = false;
				return;
			}
			if (type == "LayRect")
			{
				this.gbLayLine.Visible = false;
				this.gbLayRect.Visible = true;
				this.gbLayCross.Visible = false;
				this.tabcLayMid.Visible = false;
				this.label25.Visible = true;
				this.hytbRowAngle.Visible = true;
				return;
			}
			if (!(type == "LayMid"))
			{
				return;
			}
			this.gbLayLine.Visible = false;
			this.gbLayRect.Visible = false;
			this.gbLayCross.Visible = false;
			this.tabcLayMid.Visible = true;
			this.label25.Visible = false;
			this.hytbRowAngle.Visible = false;
		}

		private void rdoLayCorss_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoLayCorss.Checked)
			{
				this.SetControlDisplay("LayCross");
			}
		}

		private void rdoLayRect_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoLayRect.Checked)
			{
				this.SetControlDisplay("LayRect");
			}
		}

		private void rdoLayMid_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoLayMid.Checked)
			{
				this.SetControlDisplay("LayMid");
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.rdoLayLine.Checked)
			{
				this.connectType = "Line";
				if (this.rdoLimitDistance.Checked)
				{
					this.layLineParameter.LimitFlag = 2;
					this.layLineParameter.Distance = double.Parse(this.hytbDistance.Text.Trim());
					this.layLineParameter.DistanceSide = double.Parse(this.hytbDistanceSide.Text.Trim());
				}
				else if (this.rdoLimitNumber.Checked)
				{
					this.layLineParameter.LimitFlag = 1;
					this.layLineParameter.Count = int.Parse(this.hytbCount.Text.Trim());
					this.layLineParameter.Ratio = double.Parse(this.hytbRatio.Text.Trim());
				}
				string[] texts = new string[]
				{
					this.layLineParameter.LimitFlag.ToString(),
					this.layLineParameter.Count.ToString(),
					this.layLineParameter.Ratio.ToString(),
					this.layLineParameter.Distance.ToString(),
					this.layLineParameter.DistanceSide.ToString()
				};
				try
				{
					this.userSetting.WriteTerminalLayLineText(texts);
					goto IL_620;
				}
				catch (Exception)
				{
					goto IL_620;
				}
			}
			if (this.rdoLayCorss.Checked)
			{
				this.connectType = "Cross";
			}
			else
			{
				if (this.rdoLayRect.Checked)
				{
					if (this.hytbRowAngle.Text.Trim() == "")
					{
						YJKMessageBox.Information("行向角参数不能为空！");
						this.hytbRowAngle.Focus();
						return;
					}
					this.GetRectParas();
					this.connectType = "Rect";
					if (this.rdoLimitNumberRect.Checked)
					{
						this.layRectParameter.LimitFlag = 1;
						this.layRectParameter.RowCount = int.Parse(this.hytbRowCount.Text.Trim());
						this.layRectParameter.ColCount = int.Parse(this.hytbColCount.Text.Trim());
						this.layRectParameter.Ratio = double.Parse(this.hytbRectRatio.Text.Trim());
					}
					else if (this.rdoLimitDistanceRect.Checked)
					{
						this.layRectParameter.LimitFlag = 2;
						this.layRectParameter.RowDistance = double.Parse(this.hytbRowDistance.Text.Trim());
						this.layRectParameter.ColDistance = double.Parse(this.hytbColDistance.Text.Trim());
						this.layRectParameter.RowDistanceSide = double.Parse(this.hytbRowDistanceSide.Text.Trim());
						this.layRectParameter.ColDistanceSide = double.Parse(this.hytbColDistanceSide.Text.Trim());
					}
					string[] texts2 = new string[]
					{
						this.layRectParameter.LimitFlag.ToString(),
						this.layRectParameter.RowCount.ToString(),
						this.layRectParameter.ColCount.ToString(),
						this.layRectParameter.Ratio.ToString(),
						this.layRectParameter.RowDistance.ToString(),
						this.layRectParameter.ColDistance.ToString(),
						this.layRectParameter.RowDistanceSide.ToString(),
						this.layRectParameter.ColDistanceSide.ToString(),
						this.hytbRowAngle.Text.Trim()
					};
					try
					{
						this.userSetting.WriteTerminalLayRectText(texts2);
						goto IL_620;
					}
					catch (Exception)
					{
						goto IL_620;
					}
				}
				if (this.rdoLayMid.Checked)
				{
					this.connectType = "Mid";
					if (this.tabcLayMid.SelectedTab.Name == "tpGrid")
					{
						this.layMidParameter.Type = 0;
					}
					else if (this.tabcLayMid.SelectedTab.Name == "tpDistance")
					{
						this.layMidParameter.Type = 1;
					}
					this.layMidParameter.RowCount = int.Parse(this.hytbMidRowCount.Text.Trim());
					this.layMidParameter.ColCount = int.Parse(this.hytbMidColCount.Text.Trim());
					this.layMidParameter.TopDistance = double.Parse(this.hytbTopDistance.Text.Trim());
					this.layMidParameter.BottomDistance = double.Parse(this.hytbBottomDistance.Text.Trim());
					this.layMidParameter.LeftDistance = double.Parse(this.hytbLeftDistance.Text.Trim());
					this.layMidParameter.RightDistance = double.Parse(this.hytbRightDistance.Text.Trim());
					this.layMidParameter.RowDistance = double.Parse(this.hytbRow.Text.Trim());
					this.layMidParameter.ColDistance = double.Parse(this.hytbCol.Text.Trim());
					string[] texts3 = new string[]
					{
						this.layMidParameter.RowCount.ToString(),
						this.layMidParameter.ColCount.ToString(),
						this.layMidParameter.TopDistance.ToString(),
						this.layMidParameter.BottomDistance.ToString(),
						this.layMidParameter.LeftDistance.ToString(),
						this.layMidParameter.RightDistance.ToString(),
						this.layMidParameter.RowDistance.ToString(),
						this.layMidParameter.ColDistance.ToString(),
						this.layMidParameter.Type.ToString()
					};
					try
					{
						this.userSetting.WriteTerminalLayMidText(texts3);
					}
					catch (Exception)
					{
					}
				}
			}
			IL_620:
			try
			{
				this.userSetting.WriteTerminalLayText(this.connectType);
			}
			catch (Exception)
			{
				throw;
			}
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void rdoLimitNumberRect_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoLimitNumberRect.Checked)
			{
				this.rdoLimitDistanceRect.Checked = false;
				this.RdoNumberGroup(true);
				return;
			}
			this.RdoNumberGroup(false);
		}

		private void RdoNumberGroup(bool check)
		{
			this.hytbRowCount.Enabled = check;
			this.hytbColCount.Enabled = check;
			this.hytbRectRatio.Enabled = check;
		}

		private void RdoDistanceGroup(bool check)
		{
			this.hytbRowDistance.Enabled = check;
			this.hytbColDistance.Enabled = check;
			this.hytbRowDistanceSide.Enabled = check;
			this.hytbColDistanceSide.Enabled = check;
		}

		private void rdoLimitDistanceRect_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoLimitDistanceRect.Checked)
			{
				this.rdoLimitNumberRect.Checked = false;
				this.RdoDistanceGroup(true);
				return;
			}
			this.RdoDistanceGroup(false);
		}

		private void RdoNumberGroupLine(bool check)
		{
			this.hytbCount.Enabled = check;
			this.hytbRatio.Enabled = check;
		}

		private void RdoDistanceGroupLine(bool check)
		{
			this.hytbDistance.Enabled = check;
			this.hytbDistanceSide.Enabled = check;
		}

		private void rdoLimitDistance_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoLimitDistance.Checked)
			{
				this.rdoLimitNumber.Checked = false;
				this.RdoDistanceGroupLine(true);
				return;
			}
			this.RdoDistanceGroupLine(false);
		}

		private void rdoLimitNumber_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoLimitNumber.Checked)
			{
				this.rdoLimitDistance.Checked = false;
				this.RdoNumberGroupLine(true);
				return;
			}
			this.RdoNumberGroupLine(false);
		}

		private void LayAreaFormNew_Load(object sender, EventArgs e)
		{
			this.initUserSetting();
		}

		private void initUserSetting()
		{
			try
			{
				string terminalLayText = this.userSetting.GetTerminalLayText();
				this.initCheckRdo(terminalLayText);
			}
			catch (Exception)
			{
			}
		}

		private void initCheckRdo(string type)
		{
			string[] array;
			if (type == "Line")
			{
				this.rdoLayLine.Checked = true;
				array = this.userSetting.GetTerminalLayLineText();
				this.connectType = "Line";
				this.hytbDistance.Text = array[3];
				this.hytbDistanceSide.Text = array[4];
				this.hytbCount.Text = array[1];
				this.hytbRatio.Text = array[2];
				if (array[0] == "2")
				{
					this.rdoLimitDistance.Checked = true;
					this.layLineParameter.LimitFlag = 2;
				}
				else if (array[0] == "1")
				{
					this.rdoLimitNumber.Checked = true;
					this.layLineParameter.LimitFlag = 1;
				}
				this.layLineParameter.Distance = double.Parse(this.hytbDistance.Text.Trim());
				this.layLineParameter.DistanceSide = double.Parse(this.hytbDistanceSide.Text.Trim());
				this.layLineParameter.Count = int.Parse(this.hytbCount.Text.Trim());
				this.layLineParameter.Ratio = double.Parse(this.hytbRatio.Text.Trim());
				return;
			}
			if (type == "Cross")
			{
				this.rdoLayCorss.Checked = true;
				return;
			}
			if (type == "Rect")
			{
				this.rdoLayRect.Checked = true;
				array = this.userSetting.GetTerminalLayRectText();
				this.connectType = "Rect";
				this.hytbRowCount.Text = array[1];
				this.hytbColCount.Text = array[2];
				this.hytbRectRatio.Text = array[3];
				this.hytbRowDistance.Text = array[4];
				this.hytbColDistance.Text = array[5];
				this.hytbRowDistanceSide.Text = array[6];
				this.hytbColDistanceSide.Text = array[7];
				this.hytbRowAngle.Text = ((array[8] == "") ? "0" : array[8]);
				if (array[0] == "1")
				{
					this.rdoLimitNumberRect.Checked = true;
					this.layRectParameter.LimitFlag = 1;
				}
				else if (array[0] == "2")
				{
					this.rdoLimitDistanceRect.Checked = true;
					this.layRectParameter.LimitFlag = 2;
				}
				this.layRectParameter.RowCount = int.Parse(this.hytbRowCount.Text.Trim());
				this.layRectParameter.ColCount = int.Parse(this.hytbColCount.Text.Trim());
				this.layRectParameter.Ratio = double.Parse(this.hytbRectRatio.Text.Trim());
				this.layRectParameter.RowDistance = double.Parse(this.hytbRowDistance.Text.Trim());
				this.layRectParameter.ColDistance = double.Parse(this.hytbColDistance.Text.Trim());
				this.layRectParameter.RowDistanceSide = double.Parse(this.hytbRowDistanceSide.Text.Trim());
				this.layRectParameter.ColDistanceSide = double.Parse(this.hytbColDistanceSide.Text.Trim());
				return;
			}
			if (!(type == "Mid"))
			{
				this.rdoLayLine.Checked = true;
				return;
			}
			this.rdoLayMid.Checked = true;
			array = this.userSetting.GetTerminalLayMidText();
			this.connectType = "Mid";
			if (array[8] == "0")
			{
				this.tabcLayMid.SelectedTab.Name = "tpGrid";
				this.layMidParameter.Type = 0;
			}
			else if (array[8] == "1")
			{
				this.tabcLayMid.SelectedTab.Name = "tpDistance";
				this.layMidParameter.Type = 1;
			}
			this.hytbMidRowCount.Text = array[0];
			this.hytbMidColCount.Text = array[1];
			this.hytbTopDistance.Text = array[2];
			this.hytbBottomDistance.Text = array[3];
			this.hytbLeftDistance.Text = array[4];
			this.hytbRightDistance.Text = array[5];
			this.hytbRow.Text = array[6];
			this.hytbCol.Text = array[7];
			this.layMidParameter.RowCount = int.Parse(this.hytbMidRowCount.Text.Trim());
			this.layMidParameter.ColCount = int.Parse(this.hytbMidColCount.Text.Trim());
			this.layMidParameter.TopDistance = double.Parse(this.hytbTopDistance.Text.Trim());
			this.layMidParameter.BottomDistance = double.Parse(this.hytbBottomDistance.Text.Trim());
			this.layMidParameter.LeftDistance = double.Parse(this.hytbLeftDistance.Text.Trim());
			this.layMidParameter.RightDistance = double.Parse(this.hytbRightDistance.Text.Trim());
			this.layMidParameter.RowDistance = double.Parse(this.hytbRow.Text.Trim());
			this.layMidParameter.ColDistance = double.Parse(this.hytbCol.Text.Trim());
		}

		private void GetRectParas()
		{
			this.rectLayParas = new RectLayParamers();
			this.rectLayParas.LayType = (int)Convert.ToInt16(this.rdoLimitDistanceRect.Checked);
			this.rectLayParas.XSpace = UnitConvert.LengthToStd(Convert.ToDouble(this.hytbRowDistance.Text.Trim()) / 1000.0);
			this.rectLayParas.YSpace = UnitConvert.LengthToStd(Convert.ToDouble(this.hytbColDistance.Text.Trim()) / 1000.0);
			this.rectLayParas.XMargin = UnitConvert.LengthToStd(Convert.ToDouble(this.hytbRowDistanceSide.Text.Trim()) / 1000.0);
			this.rectLayParas.YMargin = UnitConvert.LengthToStd(Convert.ToDouble(this.hytbColDistanceSide.Text.Trim()) / 1000.0);
			this.rectLayParas.RowCount = Convert.ToInt32(this.hytbRowCount.Text.Trim());
			this.rectLayParas.ColCount = Convert.ToInt32(this.hytbColCount.Text.Trim());
			this.rectLayParas.Margin = Convert.ToDouble(this.hytbRectRatio.Text.Trim());
			this.rectLayParas.AngleRow = Convert.ToDouble(this.hytbRowAngle.Text.Trim());
			this.rectLayParas.LinkLineWay = 0;
			this.rectLayParas.DislocationWay = 0;
		}

		public string connectType = "Cross";

		public RectLayParamers rectLayParas;

		public LayLineParameter layLineParameter = new LayLineParameter();

		public LayAreaParameter layRectParameter = new LayAreaParameter();

		public LayMidParameter layMidParameter = new LayMidParameter();

		private HVACUserSetting userSetting;
	}
}
